<template>
  <div style="float: left;margin-top: 5px">
    <el-form ref="searchRef" :model="searchFormData">
      <el-form-item label="关键词" >
        <el-input  type="text" v-model="searchFormData.keyWord"></el-input>
      </el-form-item>
    </el-form>
  </div>
  <div style="height: 40px;margin-top: 5px;float: left;margin-left: 20px;">
    <el-button @click="searchClick">查询</el-button>
    <el-button @click="resetSearchClick">重置</el-button>
    <el-button @click="handleDecisionSoft('1')">启动拟态判决</el-button>
    <el-button  @click="handleDecisionSoft('0')">关闭拟态判决</el-button>
    <el-button  @click="handleDecisionSoft('2')">重启拟态判决</el-button>
  </div>
  <div style="clear: both"></div>
  <div style="height: calc(100% - 60px);">
    <Table  :show-page="true" ref="tableRef" :show-line-number="true"
            :showCheckBox="false"  :columns="columns" :data="data">
      <template  v-slot:handle="slotProps">
        <el-button  @click.stop="decision(slotProps.rowData,slotProps.rowIndex)">下发</el-button>
        <el-button  @click.stop="decisionLogs(slotProps.rowData,slotProps.rowIndex)">日志</el-button>
      </template>
    </Table>
  </div>
  <el-dialog title="裁决结果" v-model="dialogDecisionVisible" :close-on-click-modal="false"  width="80%">
    <div class="el-dialog-content-form">
      <Table  :show-page="true" ref="fileTableRef" :show-line-number="true"
              :showCheckBox="false" :url="fileUrl" :columns="fileColumns">
        <template  v-slot:handle="slotProps">
          <el-button  @click.stop="download(slotProps.rowData,slotProps.rowIndex)">下载</el-button>
        </template>
      </Table>
    </div>
  </el-dialog>
</template>

<script setup>

import Table from "@/components/plugs/table/Table.vue";
import DialogForm from "@/components/plugs/form/DialogForm.vue";
import {inject, nextTick, reactive, ref, toRaw} from "vue";
import {FormItemType} from "@/components/plugs/form/DialogFormInterface.js";
import {AXIOS_NAME} from "@/constant/constant.js";
import {ElMessage, ElMessageBox} from "element-plus";
import {deleteRow, HandleEnum} from "@/utils/utils.ts";

const fileTableRef = ref(null);
const fileUrl = ref("/file/getFilePage?businessType=7&otherType=1");

const tableRef = ref(null);
const $axios = inject(AXIOS_NAME);
const searchRef = ref(null);
const editFormRef = ref(null);
const dialogDecisionVisible= ref(false);
let handleCode = "";
const searchFormData = reactive({
  'keyWord': ''
});
const fileColumns = [
  {field:"fileName","desc": "文件名称"},
  {field:"uuidFileName","desc": "名称"},
  {field:"filePath","desc": "文件地址"},
  {field:"fileSize","desc": "文件大小"},
  {field:"fileLongSize","desc": "文件大小"},
  {field:"fileSuffix","desc": "后缀"},
  {field:"showTime","desc": "创建时间"},
  {field:"handle","desc": "操作",isSlot:true,width:100}
];
const columns = [
  {field:"decisionName","desc": "裁决名称",width: 200},
  {field:"decisionMark","desc": "裁决标识",width: 200},
  {field:"showTime","desc": "创建时间",width: 200},
  {field:"handle","desc": "操作",isSlot:true,width:150}
];

let data = [
  {"decisionName": "多数裁决","decisionMark": "Normal_vote","showTime": "2025-05-05 12:12:12",code:"1"},
  {"decisionName": "加权多数判决","decisionMark": "Weight_vote","showTime": "2025-05-05 12:12:12",code:"2"},
  {"decisionName": "快速比对判决","decisionMark": "Fast_vote","showTime": "2025-05-05 12:12:12",code:"3"}
];

let items = [];
items.push({type: FormItemType.CUSTOMIZE_SELECT,label: '判决容器',name: 'containerIds',
  url: '/deploy/getDeployedContainerList',textFiled:'containerName',valueField: "id",isMultiple:true});
items.push({type: FormItemType.INPUT_NUMBER,label:"时间",name:"time",default:0});
let settingForm = reactive({
  splitCol: 1,
  gutter: 20,
  labelWidth: 115,
  items: items,
  rules: {},
});
const searchClick = () =>{
  tableRef.value.reload(toRaw(searchFormData));
}

const resetSearchClick = () =>{
  for(const  key in searchFormData){
    searchFormData[key] = '';
  }
}

const decisionLogs = (rowData,rowIndex) => {
  fileUrl.value = '/file/getFilePage?businessType=7&otherType=' + rowData.code;
  console.log(fileUrl.value);
  dialogDecisionVisible.value = true;
  nextTick(() =>{
    fileTableRef.value.reload();
  })
}

const decision =  (rowData,rowIndex) => {
  editFormRef.value.show();
  handleCode = rowData.code;
}

const handleDecisionSoft = (state) =>{
  ElMessageBox.confirm(
      '是否继续操作拟态软件?','提示',{
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
  ).then(() => {
    $axios.post("/decision/handleDecisionSoft",{"state":state}).then(response => {
      const data = response.data;
      ElMessage.info(data.msg);
    });
  });
}

const validateSuccess = (data) => {
  if(data.containerIds == undefined || data.containerIds == ''){
    ElMessage.error("请选择容器数据!");
    return;
  }
  data.containerIds = JSON.stringify(data.containerIds);
  data.handleCode = handleCode;
  $axios.post("/decision/judgment",data).then(response => {
      const data = response.data;
      ElMessage.info(data.msg);
      if(data.code == 1){
        editFormRef.value.close();
      }
  });
}

const download = (rowData, rowIndex) => {
  $axios.post("/file/getDownloadToken").then(response =>{
    const data = response.data;
      window.open(import.meta.env.VITE_HTTP_BASE_URL  + "file/dowloadFile?id=" + rowData.id + "&downToken=" + data.data);
  });
}
</script>

<style scoped>

</style>